//
//  XXmessagefram.m
//  QQ聊天
//
//  Created by 宋 on 16/3/11.
//  Copyright © 2016年 宋. All rights reserved.
//

#import "XXmessagefram.h"
#import <UIKit/UIKit.h>
#import "XXmessages.h"
#import "NSString+XXNSStringExt.h"
@implementation XXmessagefram
//重写message的set方法
-(void)setMessage:(XXmessages *)message{
    _message = message;
    //获取屏幕的宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    //间距
    CGFloat margin = 5;
    //计算每个控件的frame和cell的行高
    //1.设置时间label的frame
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = screenW;
    CGFloat timeH = 15;
    //CGFloat timeH = screenH;
    _timeFrame = CGRectMake(timeX, timeY, timeW, timeH);
    
    //2.设置头像frame
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    CGFloat iconX = message.type == XXmessagesTypeOther?margin:screenW-margin-iconW;
    CGFloat iconY = CGRectGetMaxY(_timeFrame) + margin;
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    
    //计算消息正文的frame
    //1.先计算正文的大小
    CGSize textSize = [message.text sizeOfTextWithMaxSize:CGSizeMake(200, MAXFLOAT) font:textFont];
    //W,H值(可以改变气泡大小)
    CGFloat textW = textSize.width + 40;
    CGFloat textH = textSize.height + 30;

   
    CGFloat textX = (message.type == XXmessagesTypeOther)?CGRectGetMaxX(_iconFrame):(screenW - margin - iconW - textW);

    CGFloat textY = iconY;
    
    _textFrame = CGRectMake(textX, textY, textW, textH);
    
    
    //计算行高
//    CGFloat maxH = MAX(iconH, textH);
//    _rowHeight = maxH + margin;  会出错

    //获取头像最大的y值和正文的最大的y值，取大的y加margin就是行高

    CGFloat maxY = MAX(CGRectGetMaxY(_textFrame), CGRectGetMaxY(_iconFrame));
    _rowHeight = maxY + margin ;

}
@end
